<?php
namespace Controller;

require_once("View/quizView.php");

class mainPageController{
	
	/**
	 * @var $view (The view responsible for most output to both users and admin)
	 * @var $model (The model responsible for validating user and admin input)
	 */
	private $view;
	private $model;
	
	public function __construct(\Model\verificationModel $model){
		$this->model = $model;
		$this->view = new \View\quizView();
	}
	
	/**
	 * @return boolean (Wether or not user is logged in)
	 */
	public function userIsLoggedIn(){
		return $this->model->verifyUserCredentials($this->view->getUserName(), $this->view->getPassWord());
	}
	
	/**
	 * @return boolean (Wether or not admin is logged in)
	 */
	public function adminIsLoggedIn(){
		return $this->model->verifyAdminCredentials($this->view->getUserName(), $this->view->getPassWord());
	}
	
	/**
	 * @param $quizName (The name of the quiz)
	 * @param $numOfQs (Number of questions in the quiz)
	 * @return boolean (Wether or not the quiz input is validated)
	 * @throws \Exception $e (If the quiz information was not validated)
	 */
	public function verifyQuizInput($quizName, $numOfQs){
		try{
			return $this->model->verifyQuizInput($quizName, $numOfQs);
		}
		catch(\Exception $e){
			throw $e;
		}
	}
	
	/**
	 * @param $string (The string to be validated)
	 * @return boolean (True if the string was validated)
	 * @throws \Exception $e (If the string was not validated)
	 */
	public function validateString($string){
		try{
			return $this->model->validateString($string);
		}
		catch(\Exception $e){
			throw $e;
		}
	}
	
	/**
	 * @param $array (An array with strings to be validated)
	 */
	public function validateArray($array){
		try{
			foreach($array as $string){
				$this->validateString($string);
			}
		}
		catch(\Exception $e){
			throw $e;
		}
	}
	
	/**
	 * Function for handling error messages during first part of quiz creation.
	 */
	public function createQuizStageOneFailed(){
		$this->view->createQuizStageOneFailed();
	}
	
	/**
	 * Function for handling error messages during second part of quiz creation.
	 */
	public function createQuizStageTwoFailed(){
		$this->view->createQuizStageTwoFailed();
	}
	 
	 /**
	  * @return boolean (Checks if someone tries to login)
	  */
	 public function loginAttempt(){
	 	return $this->view->loginAttempt();
	 }
	
	/**
	 * @return boolean (Checks if Admin want to create a new quiz)
	 */
	 public function createNewQuizAttempt(){
	 	return $this->view->createNewQuizAttempt();
	 }
	 
	 /**
	  * @return boolean (Checks if admin moves to the next step in creating a quiz)
	  */
	  public function createNewQuizStageTwo(){
	  	return $this->view->createNewQuizStageTwo();
	  }
	  
	/**
	 * @return boolean (Checks if admin finishes creating the quiz)
	 */
	public function finishQuizCreation(){
		return $this->view->finishQuizCreation();
	} 
	 
	/**
	 * @return string (html for loginpage)
	 */
	public function getLoginPage(){
		return $this->view->getLoginPage();
	}	
		
	/**
	 * @return string (html for admin interface)
	 */
	 public function getAdminPage(){
	 	return $this->view->getAdminPage();
	 }
	
	/**
	 * @return string (html for quiz list)
	 */
	public function getQuizList(){
		return $this->view->getQuizList();
	}
	
	/**
	 * @return string (html for creating a new quiz)
	 */
	public function getCreateQuizPage(){
	 	return $this->view->getCreateQuizPage();
	}
	 
	/**
	 * @return string (html for second page in creation of quiz)
	 */
	public function getCreateQuizPageTwo($quizName){
	 	return $this->view->getCreateQuizPageTwo($quizName);
	}
	 
	/**
	 * @return string (Quizname)
	 */
	public function getQuizName(){
		return $this->view->getQuizName();
	}	
	/**
	 * @return integer (number of questions in quiz)
	 */
	public function getNumberOfQuestions(){
		return $this->view->getNumberOfQuestions();
	}
	
	/**
	 * @param $i (The index for the question)
	 * 
	 * @return string (The question at the given index)
	 */
	public function getSelectedQuestion($i){
		return $this->view->getSelectedQuestion($i);
	}
	
	/**
	 * @param $i (The index for the answer)
	 * 
	 * @return string (The answer at the given index)
	 */
	public function getSelectedAnswer($i){
		return $this->view->getSelectedAnswer($i);
	}
	
	//TODO: REFACTOR THESE THREE FUNCTIONS OR MAKE OTHER CHANGES, THIS IS NOT GOOD
	/**
	 * @param $i (Index)
	 * 
	 * @return string (The selected answeroption)
	 */
	public function getSelectedAnswerOption1($i){
		return $this->view->getSelectedAnswerOption1($i);
	}
	
	/**
	 * @param $i (Index)
	 * 
	 * @return string (The selected answeroption)
	 */
	public function getSelectedAnswerOption2($i){
		return $this->view->getSelectedAnswerOption2($i);
	}
	
	/**
	 * @param $i (Index)
	 * 
	 * @return string (The selected answeroption)
	 */
	public function getSelectedAnswerOption3($i){
		return $this->view->getSelectedAnswerOption3($i);
	}
	
	/**
	 * @return string (html for questionform)
	 */
	public function generateQuestions($i){
		return $this->view->generateQuestions($i);
	}
	
	/**
	 * @return string (html)
	 */
	public function closeForm(){
		return $this->view->closeForm();
	}
}
